import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const routes = [
	{
		path: '/',
		component: () => import('@/views/home/index')
	}
]
const modulesFiles = require.context('./modules', true, /\.js$/)
// 自动引入module包
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
	const moduleRouter = modulesFiles(modulePath)
	modules = modules.concat(moduleRouter.default)
	return modules
}, [])
const packageName = require('../../package.json').name
const createRouter = () =>
	new Router({
		mode: 'history',
		base: window.__POWERED_BY_QIANKUN__ ? `/${packageName}` : '/',
		scrollBehavior: () => ({ y: 0 }),
		routes: [...routes, ...modules]
	})

const router = createRouter()
if (!window.__POWERED_BY_QIANKUN__) {
	router.addRoutes([
		{
			path: '/login',
			component: () => import('@/views/login/index')
		}
	])
}
export default router
